package table.tablemodel;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import bank.Girokonto;
import bank.Konto;

public class KontoUebersichtTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 100L;

    private static final String[] COLUMN_NAMES = {"Kontoart", "Kontonummer", "Kontostand"};

	private List<Konto> konten;


	public KontoUebersichtTableModel() {
	    super();
	}

	@Override
	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	@Override
	public int getRowCount() {
		return getKonten() == null ? 0 : getKonten().size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Konto konto = getKonten().get(rowIndex);
		switch (columnIndex) {
		    case 0:
		        if (konto instanceof Girokonto) {
		            return "Giro";
		        } else {
		            return "Spar";
		        }

		    case 1:
		        return new Integer(konto.getKontonummer());

		    case 2:
		        return new Double(konto.getKontostand());

		    default:
		        throw new IllegalStateException("Unknown column '" + columnIndex + "'");
		}
	}

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;

            case 1:
                return Integer.class;

            case 2:
                return Double.class;

            default:
                return Object.class;
        }
    }

	@Override
    public String getColumnName(int colNr) {
		return COLUMN_NAMES[colNr];
	}

	public List<Konto> getKonten() {
		return konten;
	}

	public void setKonten(List<Konto> aKonten) {
		konten = aKonten;
		fireTableDataChanged();
	}
}
